// 首页旅行套餐展示路由器
//引入express模块
const express = require('express');
//引入数据库连接池模块
const pool = require('../pool.js');
//创建路由器对象
const r = express.Router();
// -------------------------------------------------------------------- 
// 1.创建首页展示旅拍套餐的路由接口 get /show(测试成功！) http://127.0.0.1:8080/v1/travel/show/1
r.get('/show/:meal_id', (req, res) => {
    //获取地址栏传参 meal_id
    var _meal_id = req.params.meal_id;
    // console.log(_meal_id);
    //根据请求的meal_id查库
    var sql = `select a.meal_id,a.index_pic,a.small_pic_1,a.small_pic_2,a.small_pic_3,a.small_pic_4,a.small_pic_5,price,title,city_name,store_name from travel_meal a,travel_city b,photo_store c where a.store_id=c.store_id and a.city_id = b.city_id and meal_id = ?`;
    pool.query(sql, [_meal_id], (err, result) => {
        if (err) res.send({ code: 500, msg: "服务器端错误!" });
        if (result.length == 0) {
            res.send({ code: 400, msg: '数据库中没有相应记录' });
        } else {
            res.send(result[0]);
        }
    });
});
// 2.创建首页展示旅拍套餐的路由接口 get /show(测试成功！) http://127.0.0.1:8080/v1/travel/showall
r.get('/showall', (req, res) => {
    var sql = `select a.meal_id,a.index_pic,price,title,city_name,store_name from travel_meal a,travel_city b,photo_store c where a.store_id=c.store_id and a.city_id = b.city_id limit 7`;
    pool.query(sql, (err, result) => {
        if (err) res.send({ code: 500, msg: "服务器端错误!" });
        if (result.length == 0) {
            res.send({ code: 400, msg: '数据库中没有相应记录' });
        } else {
            res.send(result);
        }
    });
});

// 3.创建旅拍套餐-全部用户评论的接口http://127.0.0.1:5050/v1/travel/comment_style?meal_id=1
// 返回评论的种类以及每个种类下的评论个数用于显然分类导航
r.get('/comment_style', (req, res) => {
    let meal_id = req.query.meal_id;
    var sql = `select keyword,count(*) as num from travel_meal_comment where meal_id = ${meal_id} group by keyword`;
    pool.query(sql, (err, result) => {
        if (err) res.send({ code: 500, msg: "服务器端错误!" });
        if (result.length == 0) {
            res.send({ code: 400, msg: '数据库中没有相应记录' });
        } else {
            res.send(result);
        }
    });
});

// 4.分类查询用户评论的接口http://127.0.0.1:5050/v1/travel/comment?meal_id=1&keyword=全部
// 返回评论的种类以及每个种类下的评论个数用于显然分类导航
r.get('/comment', (req, res) => {
    var meal_id = req.query.meal_id;
    var keyword = req.query.keyword;
    var sql;
    if (keyword == '全部') {
        // 如果类型传参为全部 则不限定种类只限定meal_id
        sql = `select * from travel_meal_comment where meal_id=${meal_id} limit 10`;
    } else {
        // 如果类型为指定类型 则限定种类且限定meal_id
        sql = `select * from travel_meal_comment where meal_id=${meal_id} and keyword = '${keyword}' limit 10`;
    }
    console.log(sql);
    pool.query(sql, (err, result) => {
        if (err) res.send({ code: 500, msg: "服务器端错误--------!" });
        if (result.length == 0) {
            res.send({ code: 400, msg: '数据库中没有相应记录' });
        } else {
            res.send(result);
        }
    });
});
// --------------------------------------------------------------------
//导出用户路由模块
module.exports = r;